'use strict';

/**
 * copyright (c) 2017-2023 厦门风领科技有限公司
 * https://www.fengling.cn/
 * 
 * 初始化配置文件
 * zengbinsi
 * 2020-08-24
 */


import path from 'path';
import pathUtil from '../libs/utils/path-utils';
import fileUtil from '../libs/utils/file-utils';
import {callSequence} from '../libs/utils/function-call-seq';
const srcPath = pathUtil.getPathInTemplateAssets(path.join('bundles', 'config'));
const dstPath = pathUtil.getPathInAssets(path.join('bundles', 'config'));

const updateDir = async (callback?: Function) => {
    await pathUtil.refreshAsset('db://assets/bundles/config/');
    console.log('[tool-box]config/ refreshed!');
    callback && callback();
};

// 初始化目录
const initDirFn = (callback?: Function)=>{
    console.log('[tool-box]initDir config...');
    pathUtil.mkdirsSyncInAssets('bundles/config', callback);
};

const initFileAppBaseConfig = async (callback?: Function) => {
    console.log('[tool-box]import AppBaseConfig.ts...');
    fileUtil.copy(path.join(srcPath, 'AppBaseConfig.tsz'), path.join(dstPath, 'AppBaseConfig.ts'));
    await pathUtil.refreshAsset('db://assets/bundles/config/AppBaseConfig.ts');
    callback && callback();
};

const initFileAppConfigFn = async (callback?: Function) => {
    console.log('[tool-box]import AppConfig.ts...');
    fileUtil.copy(path.join(srcPath, 'AppConfig.tsz'), path.join(dstPath, 'AppConfig.ts'));
    await pathUtil.refreshAsset('db://assets/bundles/config/AppConfig.ts');
    callback && callback();
};

const initFileGameConfigFn = async (callback?: Function) => {
    console.log('[tool-box]import GameConfig.ts...');
    fileUtil.copy(path.join(srcPath, 'GameConfig.tsz'), path.join(dstPath, 'GameConfig.ts'));
    await pathUtil.refreshAsset('db://assets/bundles/config/GameConfig.ts');
    callback && callback();
};

const initFileGameDataCenterFn = async (callback?: Function) => {
    console.log('[tool-box]import GameDataCenter.ts...');
    fileUtil.copy(path.join(srcPath, 'GameDataCenter.tsz'), path.join(dstPath, 'GameDataCenter.ts'));
    await pathUtil.refreshAsset('db://assets/bundles/config/GameDataCenter.ts');
    callback && callback();
};

const initFileUIManagerConfigFn = (callback?: Function) => {
    return callSequence([initDir, updateDir, async (cb?: Function) => {
        console.log('[tool-box]import UIManagerConfig.ts...');
        fileUtil.copy(path.join(srcPath, 'UIManagerConfig.tsz'), path.join(dstPath, 'UIManagerConfig.ts'));
        await pathUtil.refreshAsset('db://assets/bundles/config/UIManagerConfig.ts');
        cb && cb();
    }, callback]);
};

const initFileI18nConfigFn = async (callback?: Function) => {
    console.log('[tool-box]import I18nConfig.ts...');
    fileUtil.copy(path.join(srcPath, 'I18nConfig.tsz'), path.join(dstPath, 'I18nConfig.ts'));
    await pathUtil.refreshAsset('db://assets/bundles/config/I18nConfig.ts');
    callback && callback();
};

export const initDir = initDirFn;
export const initFileAppConfig = initFileAppConfigFn;
export const initFileGameConfig = initFileGameConfigFn;
export const initFileGameDataCenter = initFileGameDataCenterFn;
export const initFileUIManagerConfig = initFileUIManagerConfigFn;
export const initFileI18nConfig = initFileI18nConfigFn;



export const initAllConfig = (callback?: Function)=>{
    return callSequence([initDirFn, updateDir, initFileAppBaseConfig, initFileAppConfigFn, initFileGameConfigFn, initFileGameDataCenterFn, callback]);
};


